import pdb
import os
import sys
import platform
import subprocess
import shutil

from Parser import *

SBF_FILE = 'fbx_module.sbf'

PATH_BINDING_SCRIPT = os.path.abspath(os.curdir)
PATH_BUILD = os.path.join(PATH_BINDING_SCRIPT, 'build')
PATH_BIN = os.path.join(PATH_BINDING_SCRIPT, 'bin')

PATH_SIP_SOURCE = 'C:/Users/Tempo/Desktop/sip-4.12.1'
PATH_FBX_LIB = 'C:/Program Files/Autodesk/FBX/FbxSdk/2012.1/lib/vs2008/x86'
PATH_BINDING_SOURCE = 'binding_source' 
PATH_CPP_HEADER = 'C:/Program Files/Autodesk/FBX/FbxSdk/2012.1/include'

FILENAME_MODULE = os.path.join(PATH_BINDING_SOURCE, 'fbx_module.sip')
FILENAME_SBF = os.path.join(PATH_BUILD, SBF_FILE)

EXTRA_LIBS = ['fbxsdk-2012.1-md', 'Advapi32', 'Wininet']

def IsWindows():
    return platform.system() == 'Windows' or platform.system() == 'Microsoft'

def IsLinux():
    return platform.system() == 'Linux'

# Only on Windows
def GetBuildingToolPath():
    PATH_VC90_COMMON_TOOL = os.path.expandvars('$VS90COMNTOOLS')
    PATH_VC90_X86_BATCH = os.path.join(PATH_VC90_COMMON_TOOL, '../../VC/bin/vcvars32.bat')
    return PATH_VC90_X86_BATCH

def BuildSIP():
    if not os.path.exists(PATH_BUILD):
        os.makedirs(PATH_BUILD)

    sys.path.append(PATH_BUILD)
    try:
        import sipconfig
    except ImportError:
        os.chdir(PATH_SIP_SOURCE)
        command = ' '.join(['"' + sys.executable + '"', 'configure.py',
            '-b', PATH_BUILD, '-d', PATH_BUILD, '-e', PATH_BUILD])
        os.system(command)

        if IsWindows():
            batch_file = open('compile.bat', 'w')
            batch_file.write('call "' + GetBuildingToolPath() + '"\n')
            batch_file.write('nmake clean\n')
            batch_file.write('nmake\n')
            batch_file.write('nmake install\n')
            batch_file.close()
            subprocess.call('compile.bat', shell=True)

def GenerateBindingSourceFromCppHeader():
    cpp_header_filename = 'kfbxquaternion.h'
    sip_filename = cpp_header_filename.partition('.')[0] + '.sip'

    f = HeaderFile(os.path.join(PATH_CPP_HEADER, cpp_header_filename))
    if not os.path.exists(PATH_BINDING_SOURCE):
        os.makedirs(PATH_BINDING_SOURCE)
    dest_file = open(os.path.join(PATH_BINDING_SOURCE, sip_filename), 'w')
    dest_file.write(f.asSIP())
    dest_file.close()

def GenerateCppSourceFromBindingSource():
    os.chdir(PATH_BUILD)

    import sipconfig
    config = sipconfig.Configuration()
    build_macros = config.build_macros()

    build_macros['CXXFLAGS_WARN_ON'] = ''
    if IsWindows():
        config.platform = 'win32-msvc2008'

    config.set_build_macros(build_macros)

    commond = ' '.join(['"' + config.sip_bin + '"', '-c', PATH_BUILD, '-b', FILENAME_SBF,
        '-I', PATH_BINDING_SOURCE, FILENAME_MODULE])
    os.system(commond)

    makefile = sipconfig.SIPModuleMakefile(config, SBF_FILE, dir = PATH_BUILD)
    makefile.extra_include_dirs.append(PATH_CPP_HEADER)
    if IsWindows():
        makefile.extra_lib_dirs.append(PATH_FBX_LIB)
    makefile.extra_libs = EXTRA_LIBS
    pdb.set_trace()
    makefile.generate()

def BuildBindingSource():
    if IsWindows():
        batch_file = open('compile.bat', 'w')
        batch_file.write('call "' + GetBuildingToolPath() + '"\n')
        batch_file.write('nmake clean\n')
        batch_file.write('nmake\n')
        batch_file.close()
        subprocess.call('compile.bat', shell=True)

def CopyToBin():
    if not os.path.exists(PATH_BIN):
        os.makedirs(PATH_BIN)

    shutil.copy('fbx.pyd', os.path.join(PATH_BIN, 'fbx.pyd'))
    shutil.copy('sip.pyd', os.path.join(PATH_BIN, 'sip.pyd'))

if __name__ == '__main__':
    BuildSIP()
    # Binding source are files with SIP extensions
    #GenerateBindingSourceFromCppHeader()
    GenerateCppSourceFromBindingSource()
    BuildBindingSource()
    CopyToBin()
